package com.zhl.common.base;

import com.zhl.common.enums.SystemEnums;
import com.zhl.common.enums.IEnum;
import lombok.Data;


@Data
public class R<T> {
    /**
     * 业务状态码
     */
    private Integer code;

    /**
     * 返回信息
     */
    private String msg;

    /**
     * 有效数据
     */
    private T data;

    public R() {
        this.code = SystemEnums.SUCCESS.getCode();
        this.msg = SystemEnums.SUCCESS.getMsg();
    }

    public R(T data) {
        this.code = SystemEnums.SUCCESS.getCode();
        this.msg = SystemEnums.SUCCESS.getMsg();
        this.data = data;
    }

    public R(IEnum enums) {
        this.code = (Integer) enums.getCode();
        this.msg = (String) enums.getMsg();
    }

    public R(IEnum enums, T data) {
        this.code = (Integer) enums.getCode();
        this.msg = (String) enums.getMsg();
        this.data = data;
    }

    public R(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public R(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public R(IEnum enums, String msg, T data) {
        this.code = (Integer) enums.getCode();
        this.msg = msg;
        this.data = data;
    }

    public static<T> R<T> ok(T data){
        return new R<>(SystemEnums.SUCCESS, data);
    }

    public static<T> R<T> fail(IEnum iEnum, T data){
        return new R<>(SystemEnums.FAIL,iEnum.getMsg().toString(), data);
    }

    public static<T> R<T> fail(Integer code, String msg, T data){
        return new R<>(code,msg, data);
    }

    public static<T> R<T> fail(IEnum enums, String msg, T data){
        return new R<>(enums,msg, data);
    }
}
